/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.figures;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Rectangle;

/**
 * @author frankd@ifi.uio.no
 */
public class ContinuationFigure extends AnchoringFigure  {

    private static final int HEIGHT = 30;
    private static final int DEFAULT_WIDTH = 150;
    private static final int MIN_WIDTH = 75;
    private static final int ARCH_SIZE = 30;

    private Label label;

    public ContinuationFigure() {
        ConstrainedToolbarLayout layout = new ConstrainedToolbarLayout();
        layout.setVertical(true);
        layout.setStretchMajorAxis(false);
        layout.setMajorAlignment(ToolbarLayout.ALIGN_CENTER);
        layout.setMinorAlignment(ToolbarLayout.ALIGN_CENTER);
        setLayoutManager(layout);
        setBorder(new MarginBorder(5));
        setBackgroundColor(ColorConstants.listBackground);

        label = new Label();
        add(label);
    }

    public Rectangle getPreferredBounds(Rectangle suggestedBounds) {
        int width =
                suggestedBounds.width == 0 ? DEFAULT_WIDTH
                        : (suggestedBounds.width < MIN_WIDTH ? MIN_WIDTH : suggestedBounds.width);
        return new Rectangle(suggestedBounds.x, suggestedBounds.y, width, HEIGHT);
    }

    protected void paintFigure(Graphics graphics) {
        label.setFont(this.getFont());
        label.setForegroundColor(this.getFontColor());
        graphics.setFont(this.getFont());
        graphics.setForegroundColor(this.getBorderColor());
        graphics.setBackgroundColor(this.getBackgroundColor());
        Rectangle rect = getBounds().getResized(-1, -1);
        graphics.fillRoundRectangle(rect, ARCH_SIZE, ARCH_SIZE);
        graphics.drawRoundRectangle(rect, ARCH_SIZE, ARCH_SIZE);
    }

    protected boolean useLocalCoordinates() {
        return true;
    }

    // --- ILabeledFigure ---

    public Rectangle getLabelBounds() {
        Rectangle rect = label.getClientArea().getCopy();
        label.translateToAbsolute(rect);
        return rect;
    }

    public void setLabel(String labelText) {
        label.setText(labelText);
    }

    // --- Anchor contribution ---

    // Continuations remove anchors from the lifelines in the area
    // that they cover.

    public AnchorPolicy getAnchorPolicy() {
        return null;
    }

    // TODO Return rectangle in absolute coordinates
    public Rectangle getAnchorContributionBounds(IFigure covered) {
        // Note. This calculation assumes the covered figure has the same
        // parent as this continuation figure. Would be safer to
        // use absolute coordinates.
        Rectangle ref = covered.getBounds();
        return getBounds().getTranslated(-ref.x, -ref.y).resize(0, Grid.RESERVE);
    }

    public int[] contributeAnchors(IFigure figure, int y) {
        return AnchorContributor.NO_CONTRIBUTIONS;
    }
}
